package com.dankegongyu.lib.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.util.DisplayMetrics;

import androidx.annotation.NonNull;

/**
 * @author wupuquan
 * @version 1.0
 * @since 2018/8/24 11:59
 */
public class DKDialogUtil {

    private DKDialogUtil() {
    }

    /**
     * 可以分别设置四个角的Corner
     *
     * @param radiusArray 长度为8 1、2两个参数表示左上角，3、4表示右上角，5、6表示右下角，7、8表示左下角
     * @param color       背景色
     */
    public static Drawable get4SideCornerShape(Context context, float[] radiusArray, int color) {
        if (null == radiusArray || radiusArray.length != 8) {
            radiusArray = null;
        } else {
            for (int i = 0, length = radiusArray.length; i < length; i++) {
                radiusArray[i] = dp2px(context, radiusArray[i]);
            }
        }
        GradientDrawable shape = new GradientDrawable();
        shape.setCornerRadii(radiusArray);
        shape.setColor(color);
        return shape;
    }

    public static int getScreenWidth(@NonNull Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return displayMetrics.widthPixels;
    }

    /**
     * dp转px
     *
     * @param dpValue dp值
     * @return px值
     */
    public static int dp2px(Context context, final float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static boolean isActivityFinishingOrDestroy(Activity activity) {
        return activity == null || activity.isFinishing() || activity.isDestroyed();
    }
}
